import BaseTable from '../../TableTemplate/TableTemplate.vue';
export default {
    name: 'BudgetTemplate',
    props: ['data'],
    components: {
        BaseTable
    },
    data() {
        return {
            tableConfig: [
                {
                    prop: 'projectNumber',
                    label: '项目编号',
                    width: '120',
                    type: 'select',
                    style: 'width:100px',
                    options: [],
                    remote: true
                },
                { prop: 'projectName', label: '项目名称', width: '100', showTips: true },
                { prop: 'featureDescription', label: '特征描述', width: '100', showTips: true },
                { prop: 'unit', label: '单位', width: '100' },
                {
                    prop: 'covertItem',
                    label: '是否隐藏项',
                    width: '100',
                    formatter(val) {
                        const itemMap = ['否', '是'];
                        return itemMap[val];
                    }
                },
                {
                    prop: 'quantities',
                    label: '工程量',
                    type: 'input',
                    width: '100'
                },
                {
                    prop: 'integratedUnitPrice',
                    label: '综合单价',
                    type: 'input',
                    disabled: true,
                    width: '100'
                },
                {
                    prop: 'totalPrice',
                    label: '综合总价',
                    type: 'input',
                    disabled: true,
                    width: '100'
                },
                {
                    prop: 'materialsBrand',
                    label: '主材品牌',
                    disabled: true,
                    width: '100'
                },
                {
                    prop: 'remarks',
                    label: '备注',
                    type: 'input',
                    width: '100'
                },
                {
                    prop: 'approvalOpinion',
                    label: '审核意见',
                    width: '100'
                }
            ]
        };
    },
    mounted() {
        // 初始化模糊搜索回调处理
        this.initRemoteSelect();
        // 初始哈单价和综合量变化时的回调
        this.initQuantityReact();
    },
    methods: {
        initRemoteSelect() {
            const vm = this;
            const column = this.tableConfig.find(v => v.remote);
            column.remoteChange = async val => {
                // 输入编号字符数6个以上才开始查询
                if (val.length < 6) {
                    column.options = [];
                    return;
                }
                const yearBudgetNumber = this.data.yearBudgetNumber;
                const { data } = await this.$httpExt().post('/api/budget/selectDetailByParam', {
                    projectNumber: val,
                    yearBudgetNumber
                });
                if (!data) return;
                column.options = data.map(v => ({ label: v.projectNumber, value: v.projectNumber, ...v }));
            };
            column.change = (index, options, val) => {
                const rowData = options.find(v => v.value === val);
                vm.data.budgetDetailTemplate[index] = rowData;
                vm.$forceUpdate(); // 强制组件更新，防止总价不重新计算
            };
        },
        // 为工程量、综合单价输入框注册input事件
        initQuantityReact() {
            const vm = this;
            let column = vm.tableConfig.find(v => v.prop === 'quantities');
            column.change = (val, { row, $index }) => {
                vm.data.budgetDetailTemplate[$index].totalPrice = (val * (row.integratedUnitPrice || 0)).toFixed(2);
            };
            column = vm.tableConfig.find(v => v.prop === 'integratedUnitPrice');
            column.change = (val, { row, $index }) => {
                vm.data.budgetDetailTemplate[$index].totalPrice = (val * (row.quantities || 0)).toFixed(2);
            };
        },
        summaryChange(sum) {
            const vm = this;
            let total = Number(sum);
            const taxMap = { 1: 0, 2: 1, 3: 3, 4: 6, 5: 9 };
            const { budgetDetailCustom = [] } = vm.data;
            budgetDetailCustom.forEach(v => (total += Number(v.totalPrice)));
            vm.data.amountExcludingTax = total.toFixed(2);
            const tax = Number(taxMap[vm.data.invoiceTaxRate || 1]);
            // 税率未勾选，默认按照0进行处理
            vm.data.projectBudget = (((100 + tax) / 100) * total).toFixed(2);
        }
    }
};
